package cn.nbxz.dao;

import cn.nbxz.domain.Category;
import cn.nbxz.domain.Goods;
import cn.nbxz.util.JdbcHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDao implements Dao<Goods,Integer> {

    private static final String table = " t_goods " ;
    @Override
    public Goods load(Integer id) {
        String SQL="select id , name , price , image ,category_id from "+table+" where id= ? ";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , id ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Goods g =  this.cast( map );
            return g ;
        }
        return null;
    }
    public Goods load(String name){
        String SQL="select id , name , price , image ,category_id from "+table+" where name= ? ";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , name ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Goods g =  this.cast( map );
            return g ;
        }
        return null;
    }

    @Override
    public List<Goods> list() {
        String SQL="select id , name , price , image ,category_id from "+table;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL);
        helper.destory();
        if( list != null && list.size() > 0  ) {
            List<Goods> goods= new ArrayList<>();


            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);
                Goods c =   this.cast( map );
                goods.add(c);
            }
            return goods;

        }

        return null;
    }

    @Override
    public Goods cast(Map<String, Object> map) {
        Goods goods=new Goods();

        goods.setId((Integer)map.get("id"));
        goods.setName((String)map.get("name"));
        goods.setPrice((double)map.get("price"));
        goods.setImage((String)map.get("image"));

        Category category=new CategoryDao().load((Integer)map.get("category_id"));
        goods.setCategory(category);


        return goods;
    }

    @Override
    public boolean save(Goods g) {
        String SQL="insert into "+table+" ( name , price , image , category_id ) "
                +" values(?,?,?,?)";
       Integer category_id= g.getCategory().getId();
       Object[] array={g.getName(),g.getPrice(),g.getImage(),category_id};
        JdbcHelper helper = new JdbcHelper();
        int id = helper.insert( SQL , array );
        g.setId(id);
        helper.destory();

        return id != -1 ;

    }

    @Override
    public boolean update(Goods g) {
        String SQL="update "+table+" set name=?, price=? , image=? , category_id=? "
                +" where id=?";
        Integer category_id= g.getCategory().getId();
        Object[] array={g.getName(),g.getPrice(),g.getImage(),category_id,g.getId()};
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , array );
        helper.destory();

        return count == 1 ;
    }

    @Override
    public boolean delete(Integer id) {
        String SQL = "DELETE FROM " + table + " WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , id );
        helper.destory();
        return count == 1 ;


    }
}
